package org.enwey.icarenew.Activity.Login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.API.RequestResult;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.Activity.MainActivity;
import org.enwey.icarenew.Manager.SettingsManager;
import org.enwey.icarenew.Model.User;
import org.enwey.icarenew.R;

import java.util.Hashtable;

/**
 * Created by enwey on 2014/11/23.
 */
public class LoginActivity extends BaseActivity {
    private TextView mLogin;
    private TextView mRegister;
    private TextView mRest;
    private TextView mRemember;
    private ImageView mCancel;
    private EditText mAccount;
    private EditText mPassword;
    private User mLoginUser;
    private boolean isSelect = false;
    private String phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setUp();
        findView();
        registerListener();
        autoFillForm();
    }

    public void setUp() {
        showHeader(false);
        phoneNum = getIntent().getStringExtra("phone");
    }

    private void findView() {
        mLogin = (TextView) this.findViewById(R.id.activity_login_btn);
        mRegister = (TextView) this.findViewById(R.id.activity_login_register);
        mRest = (TextView) this.findViewById(R.id.activity_login_reset);
        mRemember = (TextView) this.findViewById(R.id.activity_login_remember);
        mCancel = (ImageView) this.findViewById(R.id.activity_login_cancel);
        mAccount = (EditText) this.findViewById(R.id.activity_login_username);
        mPassword = (EditText) this.findViewById(R.id.activity_login_password);
    }

    private void registerListener() {
        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.activity_login_remember:
                        isSelect = !isSelect;
                        doRemember(isSelect);
                        break;
                    case R.id.activity_login_cancel:
                        finish();
                        break;
                    case R.id.activity_login_btn:
                        doLogin();
                        break;
                    case R.id.activity_login_register:
                        doRegister();
                        break;
                    case R.id.activity_login_reset:
                        doResetPassword();
                        break;
                    default:
                        break;
                }
            }
        };
        mLogin.setOnClickListener(register);
        mRegister.setOnClickListener(register);
        mRest.setOnClickListener(register);
        mRemember.setOnClickListener(register);
        mCancel.setOnClickListener(register);
    }

    private void doLogin() {
        String phoneNum = mAccount.getText().toString().trim();
        String passWord = mPassword.getText().toString().trim();

        if (TextUtils.isEmpty(phoneNum)) {
            ToastUtil.show(LoginActivity.this, "手机号码不能为空...");
            return;
        }

        if (TextUtils.isEmpty(passWord)) {
            ToastUtil.show(LoginActivity.this, "密码不能为空...");
            return;
        }

        // 回调登录的方法
        CallAPIUserLogin(phoneNum, passWord);
    }

    // 跳转到重设密码界面
    private void doResetPassword() {
        Intent intent = new Intent(LoginActivity.this, ResetPhoneNumber.class);
        startActivity(intent);
    }

    // 跳转到注册界面
    private void doRegister() {
        Intent intent = new Intent(LoginActivity.this, RegisterPhoneNumber.class);
        startActivity(intent);
    }

    // 记住密码
    private void doRemember(boolean isSelect) {
        if (isSelect) {
            mRemember.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_keep_password_sl, 0, 0, 0);
        } else {
            mRemember.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_keep_password, 0, 0, 0);
        }
    }

    private void CallAPIUserLogin(String phone, String password) {
        showProgressDialog("正在登录...").setCancelable(false);
        Hashtable<String, String> parameters = new Hashtable<>();
        parameters.put("account", phone);
        parameters.put("password", password);
        APIManager.getInstance(this).userLogin(parameters, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                hideProgressDialog();
                showException(error);
            }
        }, new Response.Listener<RequestResult<User>>() {
            @Override
            public void onResponse(RequestResult<User> response) {
                hideProgressDialog();

                if (hasError(response)) {
                    return;
                }

                if (response != null && response.message != null) {
                    if (response.data != null) {
                        SettingsManager.saveLoginUser(response.data);
                        SettingsManager.setPhone(response.data.account);
                        SettingsManager.setRememberPwd(isSelect,response.data.password);
                        ToastUtil.show(LoginActivity.this, response.message);
                        goToMainActivity();
                    }
                }

            }
        });
    }

    private void goToMainActivity() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    private void autoFillForm() {
        if (!TextUtils.isEmpty(phoneNum)) {
            mAccount.setText(phoneNum);
            return;
        }
        String phone = SettingsManager.getPhone();
        if (!TextUtils.isEmpty(phone)) {
            mAccount.setText(phone);
            isSelect = SettingsManager.isRememberPwd();
            if (isSelect) {
                String pwd = SettingsManager.getRememberPwd();
                mPassword.setText(pwd);
            }
            doRemember(isSelect);
        }
    }


}
